package com.nowcoder.community.controller.interceptor;

import ch.qos.logback.classic.joran.action.LoggerAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Interceptor学习
 */
//组件标记
//@Component
//实现HandlerInterceptor拦截器接口
public class AlphaInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(AlphaInterceptor.class);

//    在Controller之前执行
    /**
     * @return true 继续执行 否则中断
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        debug:只有调试时输出
//        handler:拦截的目标
        logger.debug("prehandle:"+handler.toString());
        return true;
    }

//    在Controller之后执行
//    模板(TemplateEngine)之前
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.debug("posthander:"+handler.toString());
    }
//    在Controller之后,模板之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.debug("afterCompletion:" + handler.toString());
    }
}
